<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\store\model;

use app\model\BaseModel;
use think\facade\Db;

class StoreAccount extends BaseModel
{
    public $period_types = [ 1, 2, 3 ];//转账周期类型1.天  2. 周  3. 月

    public $from_type = [
        'order' => [
            'type_name' => '门店结算',
            'type_url' => '',
        ],
        'refund' => [
            'type_name' => '订单退款',
            'type_url' => '',
        ],
        'withdraw' => [
            'type_name' => '提现',
            'type_url' => '',
        ],
    ];

    /**
     * 获取门店转账设置
     */
    public function getStoreWithdrawConfig($site_id)
    {
        $config = new Config();
        $res = $config->getStoreWithdrawConfig($site_id);
        return $res;
    }

    /**
     * 获取门店待结算订单金额
     */
    public function getWaitSettlementInfo($store_id)
    {
        $money_info = model('order')->getInfo([
            [ 'store_id', '=', $store_id ],
            [ 'order_status', '=', 10 ],
            [ 'store_settlement_id', '=', 0 ]
        ], 'sum(order_money) as order_money, sum(refund_money) as refund_money, sum(shop_money) as shop_money, sum(platform_money) as platform_money, sum(refund_shop_money) as refund_shop_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission');
        if (empty($money_info) || $money_info == null) {

            $money_info = [
                'order_money' => 0,
                'refund_money' => 0,
                'shop_money' => 0,
                'platform_money' => 0,
                'refund_shop_money' => 0,
                'refund_platform_money' => 0,
                'commission' => 0
            ];

        }
        return $money_info;
    }

    /**
     * 门店账户记录操作
     * @param $params
     */
    public function addStoreAccount($params)
    {
        $site_id = $params[ 'site_id' ];
        $store_id = $params[ 'store_id' ];
        $account_data = $params[ 'account_data' ];
        $remark = $params[ 'remark' ];
        $from_type = $params[ 'from_type' ];
        $related_id = $params[ 'related_id' ];
        $is_limit = $params[ 'is_limit' ] ?? 1;//是否限制不能小于0
        model('store_account')->startTrans();
        try {
            //账户检测
            $store_account = Db::name('store')->where([
                [ 'store_id', '=', $store_id ],
                [ 'site_id', '=', $site_id ]
            ])->field('account')->lock(true)->find();

            $account_new_data = round((float) $store_account[ 'account' ] + (float) $account_data, 2);

            if ($is_limit == 1 && (float) $account_new_data < 0) {
                model('store_account')->rollback();
                $msg = '账户余额不足';
                return $this->error('', $msg);
            }

            //添加记录
            $type_info = $this->from_type[ $from_type ];
            $data = array (
                'site_id' => $site_id,
                'store_id' => $store_id,
                'account_data' => $account_data,
                'from_type' => $from_type,
                'type_name' => $type_info[ 'type_name' ],
                'create_time' => time(),
                'remark' => $remark,
                'related_id' => $related_id,
            );

            model('store_account')->add($data);
            //账户更新
            model('store')->update([
                'account' => $account_new_data
            ], [
                [ 'store_id', '=', $store_id ]
            ]);
            model('store_account')->commit();
            return $this->success();
        } catch (\Exception $e) {
            model('store_account')->rollback();
            return $this->error('', $e->getMessage());
        }
    }

    /**
     * 获取账户分页列表
     * @param array $condition
     * @param int $page
     * @param int $page_size
     * @param string $order
     * @param string $field
     * @return array|\multitype
     */
    public function getStoreAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc,id desc', $field = '*', $alias = 'a', $join = [])
    {
        $list = model('store_account')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
        return $this->success($list);
    }

    /**
     * 获取账户列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param null $limit
     * @return array|\multitype
     */
    public function getStoreAccountList($condition = [], $field = '*', $order = '', $limit = null)
    {
        $list = model('store_account')->getList($condition, $field, $order, '', '', '', $limit);
        return $this->success($list);
    }
}